package com.itheima.reggie.contronller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.LoginCode;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/dish")
public class DishContronller {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    public R<String> saveDishWithFlavor(@RequestBody DishDto dishDto) {
        dishService.saveDishWithFlavor(dishDto);
        return R.success("菜品添加成功");
    }

    @GetMapping("/page")
    public R<Page> pageDishWithCategory(Integer page, Integer pageSize, String name) {

        Page<DishDto> dtoPage = dishService.pageDishWithCategory(page, pageSize, name);
        return R.success(dtoPage);

    }

    @GetMapping("/{id}")
    public R<DishDto> getDish(@PathVariable Long id) {
        if (id == null) {
            return R.error("id不能为空");
        }
        //根据id查找

        DishDto dishDto = dishService.getDishWithFlavor(id);

        return R.success(dishDto);
    }

    @PutMapping
    public R<String> updateDishWithFlavor(@RequestBody DishDto dishDto) {
        dishService.updateDishWithFlavor(dishDto);

        return R.success("修改成功");
    }

    /**
     * 批量删除根据dishid
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteishWithFlavor(Long[] ids) {

        dishService.deleteishWithFlavor(ids);


        return R.success("删除成功");

    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long[] ids) {
        Dish dish = new Dish();
        dish.setStatus(status);


        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.in(Dish::getId, ids);

        dishService.update(dish, lambdaQueryWrapper);

        //停售起售菜品应该把缓存里菜品对应的分类列表清空
        List<Long> idlist = new ArrayList<>();
        Collections.addAll(idlist,ids);
        List<Dish> dishList = dishService.listByIds(idlist);
        for (Dish d : dishList) {
            redisTemplate.delete(redisTemplate.keys(LoginCode.DISHLIST_REDIS_KEY + d.getCategoryId() + "*"));
        }
        Set keys1 = redisTemplate.keys(LoginCode.DISHLIST_REDIS_KEY +  "null*");
        redisTemplate.delete(keys1);


        return R.success("修改成功");
    }

    /**
     * 查询菜品以及口味信息
     * @param name
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(String name,Long categoryId,Integer status){

        List<DishDto> dishDtoList = dishService.listWithFlavor(name,categoryId,status);


        return R.success(dishDtoList);
    }
}
